<template>
  <div class="box">
    <div class="top">
        <input type="text">
    </div>
    <div class="banner">
        <div class="img">广告招租</div>
    </div>
    <h2>猜你喜欢</h2>
    <div class="nav">
        <div class="nav" v-for="(item , index) in nav" :key="index" @click="navlist(item , index)" :class="listnum == index ? 'change1' : ''">{{item}}</div>
    </div>
    <div class="list">
        <div class="lists" v-for="(item , index) in newlist ? newlist : list" :key="index">
            <div class="img"></div>
            <h4>{{item.name}}</h4>
            <div class="text">
                <h3>距离：{{item.distance}}</h3>
                <h3>月销量：{{item.sales}}</h3>
            </div>
        </div>
    </div>
    <div class="bottom" >
        <p v-for="(item , index) in bottom" :key="index" @click="bottomclick(index)" :class="bottomnum == index ? 'change2' : ''">{{item}}</p>
    </div>
  </div>
</template>

<script>
// import './two.css'
export default {
    data(){
        return {
            nav:['距离最近' , '综合排序' , '销量最高' , '筛选'],
            list:[
                {id:1 , distance : 500 , sales : 50 ,name : '河粉'},
                {id:2 , distance : 5000 , sales : 34 ,name : '棉线'},
                {id:3 , distance : 5000 , sales : 23 ,name : '肌肉'},
                {id:4 , distance : 5000 , sales : 65 ,name : '丸子'},
                {id:5 , distance : 5000 , sales : 12 ,name : '烧烤'},
                {id:6 , distance : 5460 , sales : 98 ,name : '串串'},
                {id:7 , distance : 57634 , sales : 65 ,name : '饺子'},
                {id:8 , distance : 6452 , sales : 234 ,name : '包子'},
                {id:9 , distance : 9123 , sales : 95 ,name : '米饭'},
                {id:10 , distance : 111 , sales : 46 ,name : '拉面'}
            ],
            bottom:['首页' , '订单' , '我的'],
            listnum:null,
            bottomnum:null,
            newlist:null
        }
    },
    methods:{
        navlist(item , index){
            if(this.listnum == index){
                this.listnum = null
                this.newlist = null
            }else{
                this.listnum = index
                if(index == 0){
                    this.newlist = [...this.list.map((item) => {return item.distance})]
                    this.newlist.sort(function(a,b){return a-b})
                    var arr = []
                    var newarr = [...this.list]
                    for(let i = 0 ; i < this.newlist.length ; i++){
                        if(i - 1 >= 0 ? this.newlist[i] == this.newlist[i-1] : false){
                            console.log(i);
                            var index = newarr.findIndex((item) => { return item.distance == this.newlist[i]})
                            newarr.splice(index , 1)
                            var j = newarr.findIndex((item) => { return item.distance == this.newlist[i]})
                            arr.push(newarr[j])
                        }else{
                            var index = this.list.findIndex((item) => { return item.distance == this.newlist[i]})
                            arr.push(this.list[index])
                        }
                    }
                    this.newlist = [...arr]
                }else if(index == 2){
                    this.newlist = [...this.list.map((item) => {return item.sales})]
                    this.newlist.sort(function(a,b){return b-a})
                    var arr = []
                    var newarr = [...this.list]
                    for(let i = 0 ; i < this.newlist.length ; i++){
                        if(i - 1 >= 0 ? this.newlist[i] == this.newlist[i-1] : false){
                            console.log(i);
                            var index = newarr.findIndex((item) => { return item.sales == this.newlist[i]})
                            newarr.splice(index , 1)
                            var j = newarr.findIndex((item) => { return item.sales == this.newlist[i]})
                            arr.push(newarr[j])
                        }else{
                            var index = this.list.findIndex((item) => { return item.sales == this.newlist[i]})
                            arr.push(this.list[index])
                        }
                    }
                    this.newlist = [...arr]
                }
            }
        },
        bottomclick(index){
            this.bottomnum = index
        }
    }
}
</script>

<style scoped>
    .box{
    height: 800px;
    width: 500px;
    margin: auto;
    display: flex;
    flex-direction: column;
    margin-top: 100px;
}
.top{
    width: 100%;
    box-sizing: border-box;
    padding: 5px 10px;
}
.top input {
    width: 100%;
    height: 40px;
    border: 1px solid #eee;
    border-radius: 40px;
}
.banner{
    margin:5px auto;
    width: 95%;
    height: 200px;
}
.banner .img{
    height: 100%;
    background-color: aqua;
    font-size: 30px;
    line-height: 200px;
    text-align: center;
}
.box h2{
    font-size: 20px;
    margin-top: 10px;
    padding-left: 20px;
}
.nav{
    height: 50px;
    margin-top: 10px;
    display: flex;
    justify-content: space-around;
}
.navlist{
    width: 100px;
    font-size: 14px;
    line-height: 50px;
    box-sizing: border-box;
    text-align: center;
    background-color: burlywood;
    border-radius: 5px;
}
.list{
    padding: 0 20px;
    height: 350px;
    overflow: auto;
}
.lists{
    height: 80px;
    display: flex;
}
h4{
    line-height: 80px;
    width: 100px;
    text-align: center;
    margin-left: 100px;
}
.lists .img{
    height: 60px;
    width: 60px;
    background-color: blueviolet;
    margin-top: 10px;
}
.text{
    margin-left: 50px;
}
.lists h3{
    line-height: 40px;
}
.bottom{
    height: 90px;
    display: flex;
    justify-content: space-around;
}
p{
    line-height: 90px;
}
.change1{
    color: red;
}
.change2{
    color: blueviolet;
}
</style>